package com.zc.study.java爬虫.eastMoney;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 获取网站文章并保存到本地
 */
public interface AnalysisDetailHtml {

    final static String FILE_PATH = "E:\\article\\";//保存文件夹地址
    final static String TITLE_PATH = "E:\\article\\title\\";//保存文件夹地址
    final static int Divide_path = 500;

    //解析网页,获取指定内容
    default void analysis(Map<String, BaseStore> map) {
        List<String> urlList = new ArrayList<>(map.keySet());
        int size = (EastCash.STORE_MAP.size() / Divide_path) + 1;
        for (int i = 0; i < size; i++) {
            List<String> collect = urlList.stream().skip(i * Divide_path).limit(Divide_path)
                    .parallel().collect(Collectors.toList());
            new Thread(() -> collect.forEach(url -> {
                try {
                    BaseStore art = map.get(url);
                    // 第一步：访问页面
                    Document document = null;
                    try {
                        document = Jsoup.connect(url).get();
                    } catch (IOException e) {
                        System.out.println("链接异常");
                        e.printStackTrace();
                    }
                    System.out.println("start store child inde::" + url);
                    // 第二步：解析页面
                    Elements titleElements = document.getElementsByTag("title");
                    String title = titleElements.get(0).text();
                    Elements elements = document.select("#ContentBody");
                    String content = elements.text();
                    art.setUrl(url);
                    art.setTitle(title);
                    art.setContent(content);
                    copyToLocal(art);
                    map.put(url, art);
                } catch (NullPointerException e) {
                    System.out.println(url + "读取不到文章");
                }

            })).start();
        }
        //如果解析的url
//        map.forEach((url, art) -> {
//            try {
//                // 第一步：访问页面
//                Document document = null;
//                try {
//                    document = Jsoup.connect(url).get();
//                } catch (IOException e) {
//                    System.out.println("链接异常");
//                    e.printStackTrace();
//                }
//                System.out.println("start store child inde::" + url);
//                // 第二步：解析页面
//                Elements titleElements = document.getElementsByTag("title");
//                String title = titleElements.get(0).text();
//                Elements elements = document.select("#ContentBody");
//                String content = elements.text();
//                art.setUrl(url);
//                art.setTitle(title);
//                art.setContent(content);
//                copyToLocal(art);
//                map.put(url, art);
//            } catch (NullPointerException e) {
//                System.out.println(url + "读取不到文章");
//            }
//        });
    }


    //东方财经,将string 文件流保存到本地
    default void copyToLocal(BaseStore store) {
        String fileName = (UUID.randomUUID() + ".txt").replaceAll("-", "");
        store.setFileName(fileName);
        File file = createFile(FILE_PATH, fileName);
        try (
                FileOutputStream fos = new FileOutputStream(file)
        ) {
            byte bytes[] = new byte[1024];
            bytes = store.getContent().getBytes();
            int b = bytes.length;   //是字节的长度，不是字符串的长度
            fos.write(bytes, 0, b);
            fos.write(bytes);
            fos.close();
            //将保存成功的文件追加到目录文件下
            writeTitleFile(store);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static File createFile(String path, String fileName) {
        File existsPackage = new File(path);
        if (!existsPackage.exists()) {
            existsPackage.mkdirs();
        }
        File file = new File(path + fileName);
        System.out.println("start store" + file.getName());
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }


    /**
     * 将字符串追加到文件已有内容后面
     */

    public static void writeTitleFile(BaseStore store) {

        FileOutputStream fos = null;

        try {
            File file = createFile(TITLE_PATH, "eastList.txt");
            StringJoiner con = new StringJoiner("|  ");
            String content = con.add(store.getTitle()).add(store.getUrl()).add(store.getFileName())
                    .toString();
            //true不覆盖已有内容
            fos = new FileOutputStream(file, true);
            //写入
            fos.write(content.getBytes());
            // 写入一个换行
            fos.write("\r\n".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
